class UsersController < ApplicationController
  # GET /users
  # GET /users.xml
  def index
    @users = User.find(:all ,:order=>"id")

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @users }
    end
  end

  # GET /users/1
  # GET /users/1.xml
  def show
    @user = User.find(params[:id])
    render :file => "#{RAILS_ROOT}/app/views/users/show.js.rjs"
    #respond_to do |format|
    #  format.js
    #end
  end

  # GET /users/new
  # GET /users/new.xml
  def new
    @user = User.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user }
    end
  end

  def create
    @user = User.new(params[:user])
    @user.permission = 1
    @user.punishment = 0
    respond_to do |format|
      if @user.save
        uuser = User.find_by_account(@user.account)
        session[:user_id] = uuser.id
        format.html { redirect_to :controller=>"index" }
        format.xml  { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :controller=>"users", :action => "new" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
  def edit
    @user = User.find(params[:id])
  end
  def update
    @user = User.find(params[:id])
    input_time = Time.gm(params[:punishment][:"written_on(1i)"].to_i,
      params[:punishment][:"written_on(2i)"].to_i,
      params[:punishment][:"written_on(3i)"].to_i)
    respond_to do |format|
     if(@user.update_attribute('permission', params[:permission])&&
      @user.update_attribute('punishment',input_time))
      # @user.update_attributes(params[:user])
        format.html { redirect_to :controller=>"index" }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
     format.html { redirect_to :controller=>"index" } 
    end
  end
  def edit_self
    @user = User.find(session[:user_id] )
  end
  def update_self
    @user = User.find(params[:id] )
    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to :controller=>"index" }
      else
        format.html { render :action => "edit_self"  }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
end
